<?php
/*文件缓存类*/
namespace app\common;
use \think\Cache;
class CacheData
{
	private static $data;

	//获取缓存
	public static function get_data($key){
		if(!empty(self::$data[$key])){
			return self::$data[$key];
		}
		$data = Cache::get($key);
		if($data){
			self::$data[$key] = $data;
		} else {
			$data = self::$key();
			Cache::set($key,$data);
		}
		return $data;
	}

	//获取最新缓存
	public static function re_data($key){
		Cache::rm($key);
		return self::get_data($key);
	}

	//获取地区表作为缓存
    private static function area($area=[],$id=100000,$level=0){
        static $arr = array();
        if(empty($arr)){
            $model_area = model('AreaModel');
            $area = $model_area->column('*','id');
        }
        foreach($area as $key=>$value){
            if ($value['pid']==$id) {
                $value['level'] = $level+1;
                $arr[$key] = $value;
                if(isset($arr[$id])){
                    $arr[$id]['child'][] = $key;
                }
                self::area($area,$value['id'],$value['level']);
            }
        }
        return $arr;
    }

    //后台节点缓存
    private static function node(){
        $model_Node = model('NodeModel');
        $node = $model_Node->where(['is_menu'=>1])->order('sort desc')->column('');

        foreach($node as $k=>$v){
            if( !empty( $node[$v['pid']]) ){
                $node[$v['pid']]['child'][] = $k;
            }
            if( !isset($node[$k]['child']) ){
                $node[$k]['child'] = [];
            }
            $node[$k]['href_name'] = $v['controller_name']."/".$v['action_name'];
            $node[$k]['name'] = $v['node_name'];

        }
        return $node;
    }

	
}